import requests
from 数据库 import *

agree_count = 0
concat_count = 0
zhuanpai_count = 0


def agree(taskid, instanceid, conn, cursor):
    global agree_count
    url = 'http://192.168.16.240:8001/api/Inventory/PurchaseOrder/UpdateWorkrecordById'
    data = {
        "processInstanceId": instanceid,
        "remark": "同意",
        "result": "agree",
        "actionerUserId": "021836126620-236970326",
        "taskId": taskid
    }
    res = requests.post(url=url, json=data)
    result = res.json()['data']['result']
    if res.status_code == 200 and result == True:
        agree_count += 1
        # 处理成功,将该任务id更新到数据库中
        update_taskid(conn, cursor, taskid)
        print('同意成功')

    else:
        print('同意失败')


def refuse(taskid, instanceid, message, conn, cursor):
    global concat_count
    url = 'http://192.168.16.240:8001/api/Inventory/PurchaseOrder/UpdateWorkrecordById'
    # 设置请求头
    data = {
        "processInstanceId": instanceid,
        "remark": message,
        "result": "refuse",
        "actionerUserId": "021836126620-236970326",
        "taskId": taskid
    }
    res = requests.post(url=url, json=data)
    result = res.json()['data']['result']
    if res.status_code == 200 and result == True:
        concat_count += 1
        update_taskid(conn, cursor, taskid)
        print('拒绝成功')
    else:
        print('拒绝失败')


def zhuanpai(taskid, message, conn, cursor):
    global zhuanpai_count
    print(message)
    url = 'http://192.168.16.240:8001/api/Inventory/PurchaseOrder/RedirectWorkrecordById'
    # 设置请求头
    data = {
        "taskId": taskid,
        "toUserId": "01086123170039942106",
        "remark": message,
        "operateUserId": "021836126620-236970326"
    }
    res = requests.post(url=url, json=data)
    result = res.json()['data']['result']
    if res.status_code == 200 and result == True:
        zhuanpai_count += 1
        update_taskid(conn, cursor, taskid)
        print('转派成功')
    else:
        print('转派失败')


def send_message_to_dingtalk(message):
    webhook_url = ('https://oapi.dingtalk.com/robot/send?access_token'
                   '=eb1e4541f99efd595bf2e3432dd36ffec20feebcb626bf810f4ab5a0ab9100db')
    data = {
        'msgtype': 'text',
        'text': {
            'content': message,
        }
    }
    response = requests.post(webhook_url, json=data)
    if response.status_code == 200:
        print("消息发送成功")
    else:
        print(f"消息发送失败，错误码: {response.status_code}")
